package com.yunc.common.queue.activemq;

import com.yunc.modules.chat.entity.ChatFriendsGroups;
import com.yunc.modules.chat.service.IChatFriendsGroupsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

/**
 * 接受消息
 *
 * @author tao cong cong
 * @create 2019/3/17 12:57 PM
 */
@Service
public class ActiveMQConsumer {

    private static final String CHAT_FRIEND_GROUP_DEFAULT_NAME = "我的好友";

    @Autowired
    private IChatFriendsGroupsService chatFriendsGroupsService;

    /**
     * 消费者监听队列
     */
    @JmsListener(destination = "item.queue")
    public void receiveQueue(String message) {
        // 收到通道的消息后执行
        System.out.print(message + "....................");
    }

    @JmsListener(destination = "item.queue", containerFactory="innerFactory")
    public void saasReceiveQueue(String message) {
        // 收到通道的消息后执行
        System.out.print(message + "inner..................");
    }

    @JmsListener(destination = "item.topic", containerFactory="innerTopicFactory")
    public void saasReceiveTopic(String message) {
        // 收到通道的消息后执行
        System.out.print(message + "topic..................");
    }

    /**
     * 为用户创建一个默认的群组
     */
    @JmsListener(destination = "chat.friend.group.create.queue")
    public void chatFirendGroupQueue(Long userId) {
        // 为其初始化
        ChatFriendsGroups friendsGroup = new ChatFriendsGroups();
        friendsGroup.setUserId(userId);
        friendsGroup.setName(CHAT_FRIEND_GROUP_DEFAULT_NAME);
        chatFriendsGroupsService.save(friendsGroup);
    }
}
